#! /usr/bin/env python2
# -*- coding: utf-8 -*-

#######################
#	获取失信人员名单列表
#######################

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from selenium import webdriver
from config import domains,keywords,conn
import time
import StringIO
import urllib
import re
from bs4 import BeautifulSoup
from selenium.common.exceptions import NoSuchElementException
import MySQLdb

cur = conn.cursor()

print "opening broswer..."
#driver = webdriver.Firefox()
driver = webdriver.PhantomJS(executable_path='D:/phantomjs/bin/phantomjs.exe')
#driver = webdriver.PhantomJS(executable_path='/usr/local/phantomjs')
print "getting url..."
driver.get("https://www.baidu.com/s?ie=utf-8&wd=%E8%80%81%E8%B5%96")
driver.implicitly_wait(30)
driver.set_page_load_timeout(30)
driver.set_script_timeout(30)
listinfo = []
#获取详请页面内容 处理
def dealData():
	#获取当前页面内容
	soup = BeautifulSoup(driver.page_source,"html.parser")
	#获取公司或者自然人名称列表
	iname = soup.find_all("span",class_="op_trust_name")
	#获取公司或者自然人身份证或者组织机构号
	cardNum = soup.find_all("span",class_="op_trust_fl op_trust_papers")
	for i in range(len(iname)):
		items={}
		trlist = soup.find_all("div",class_="op_trust_info")[i].find_all("tr")
		for tr in trlist:
			key = tr.find_all("td")[0]
			value = tr.find_all("td")[1]
			items[key.text.strip()] = value.text.strip()
		#listinfo.append(items)
		sql = "insert into dishonesty (iname,partyTypeName,courtName,caseCode,areaName,cardNum,performance,lastmod) values('%s','%s','%s','%s','%s','%s','%s','%s')" % (iname[i].text,items.get(u"法定代表人或者负责人姓名：",""),items.get(u"执行法院："),items.get(u"案号："),items.get(u"省份："),cardNum[i].text,items.get(u"被执行人的履行情况："),items.get(u"发布时间："))
		try:
			cur.execute(sql)
			conn.commit()
		except MySQLdb.Error,e:
			print "[data is exist！] ",sql
		#插入主表中
		insertsql = ""
	#递归判断是否下一页可以点击
	# if driver.find_element_by_class_name('op_trust_page_next').is_displayed():
	# 	driver.find_element_by_class_name('op_trust_page_next').click()
	# 	time.sleep(3)
	# 	dealData()
	# else:
	# 	return "complete"
print "deal with this data..."
print "current page : 1"
dealData()
#暂时设置循环次数
for j in range(10):
	print "deal with this data..."
	print "current page : ",j+1
	driver.find_element_by_class_name('op_trust_page_next').click()
	dealData()
	time.sleep(3)
driver.close()
driver.quit()
